mod config;
mod handlers;
mod models;
mod error;
use handlers::*;
use models::*;
use std::{ops::Deref, sync::Arc};


use axum::{Router, routing::{get, patch, post}};
pub use config::AppConfig;
use error::AppError;


#[derive(Debug, Clone)]
pub(crate) struct AppState {
    inner: Arc<AppStateInner>
}

#[allow(unused)]
#[derive(Debug)]
pub(crate) struct AppStateInner{
    pub(crate)  config: AppConfig,
}

pub fn get_router(config: AppConfig) -> Router {
    let state = AppState::new(config);

    let api =  Router::new()
    .route("/health", get(|| async {"welcome to chat app"}))
    .route("/signin", post(signin_handler))
    .route("/signup", post(signup_handler))
    .route("/chat",get(list_chat_handler).post(create_chat_handler) )
    .route("/chat/{id}",patch(update_chat_handler).delete(delete_chat_handler)
              .post(send_message_handler))
    .route("/chat/{id}/messages", get(list_message_handler));


    // sse的   可以在这里开发， 但这里分开

    

Router::new()
    .route("/", get(index_handler))
    .nest("/api", api)
    .with_state(state)


}


impl Deref  for AppState {
    type Target = AppStateInner;

    fn deref(&self)  ->&Self::Target {
        &self.inner
    }
    
}

impl AppState {
    pub fn new(config: AppConfig) -> Self{
        Self {
            inner: Arc::new(AppStateInner { config }),
        }
    }
}

